//引入组件库
var nf = sm("do_Notification");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var listdataCommunity = mm("do_ListData");
var helperOpen = require("helper/open");
var bdmapHelper = require("helper/bdmap");
var communityUtil = require("helper/community");
var joinCommunity = ui("joinCommunity");
//本页面-ajax业务请求url
var httpUrl = communityUrl.url.base.distancePage;

// 声明UI变量
var do_ALayout_root = ui("do_ALayout_root");
var headerAout = ui("headerAout");
var listViewCommunity = ui("listViewCommunity");
var labelDingweiCity = ui("labelDingweiCity");
var textFieldSearch = ui("textFieldSearch");
var aLayoutChangeCity = ui("aLayoutChangeCity");
var labelJoin=ui("labelJoin");

listViewCommunity.bindItems(listdataCommunity);

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible=true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");

header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "定位小区",
})
header.refreshData();
/*headerRight.on("touch", function() {
	helperOpen.open("source://view/community/map/showMarkers.ui", {iconUrl : "source://image/community/community.png"});
})*/

var page = 0;
var dingweiX, dingweiY, dingweiCity;
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	dingweiX=bdmapHelper.getDingwei().longitude;
	dingweiY=bdmapHelper.getDingwei().latitude;
	dingweiCity=bdmapHelper.getDingwei().cityName;
	if (null != dingweiCity && dingweiCity.trim() != '') {
		labelDingweiCity.text = dingweiCity;
	} else {
		labelDingweiCity.text = "定位失败";
	}
	loadPage();
});
var search;
textFieldSearch.on("enter", function(data, e) {
	search = this.text;
	loadPage();
});
aLayoutChangeCity.on("touch", function() {//
	helperOpen.open("source://view/area/cityMain.ui");
});
function loadPage() {
	page = 1;
	var params = {
		page : page,
		x : dingweiX,
		y : dingweiY,
		cityName : dingweiCity,
		search : search
	};
	qqHttp.post(httpUrl, params, function(data) {
		labelJoin.text=data.myCommunityBase.name;
		joinCommunity.tag = data.myCommunityBase.id;
		listViewCommunity.rebound();
		listdataCommunity.removeAll();
		listdataCommunity.addData(data.page.content);
		listViewCommunity.refreshItems();
		if (data.page.content.length <= 0) {// 没有社区
			listdataCommunity.addOne({
				template : 1
			}, 0);
			listViewCommunity.refreshItems();
		}
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page,
		x : dingweiX,
		y : dingweiY,
		cityName : dingweiCity,
		search : search
	};
	qqHttp.post(httpUrl, params, function(data) {
		listViewCommunity.rebound();
		listdataCommunity.addData(data.page.content);
		listViewCommunity.refreshItems();
	})
}
// 下拉列表，刷新数据
listViewCommunity.on("pull", function(data) {
	if (data.state == 2) {
		loadPage();
	}
});

// 上拉列表，翻页数据
listViewCommunity.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});

// 从选择城市页面进入
do_Page.on("result", function(data) {
	dingweiCity = data.cityName;
	labelDingweiCity.text = dingweiCity;
	search = "";
	textFieldSearch.text = "";
	loadPage();
});
//已加入社区
joinCommunity.on("touch","",300, function() {
	communityUtil.communityVisitId(joinCommunity.tag);
	do_App.closePage({fromPage:"distance"});
})